package cn.congzhou.crm.service.impl;

import cn.congzhou.crm.domain.School;
import cn.congzhou.crm.mapper.SchoolMapper;
import cn.congzhou.crm.service.ISchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SchoolServiceImpl implements ISchoolService {

    @Autowired
    private SchoolMapper schoolMapper;

    /**
     * 查询所有校区信息
     * @return
     */
    @Override
    public List<School> selectAll() {
        List<School> list = schoolMapper.selectAll();
        return list;
    }

    @Override
    public void saveOrUpdate(School school, Long[] deptIds) {
        if(school.getId() !=null){
            schoolMapper.updateSchool(school);
        }else{
            schoolMapper.insertSchool(school);
        }
        //维护校区和部门的关系，先删除关系然后再添加
        schoolMapper.deleteDepartmentRelation(school.getId());
        if(deptIds != null && deptIds.length > 0){
            for (Long deptId : deptIds) {
                schoolMapper.insertDepartmentRelation(school.getId(),deptId);
            }
        }

    }
}
